<script setup lang="ts">
import { defineProps } from 'vue'
import organizationtable from "./organizationtable.vue"
import Userlisttable from "./Userlisttable.vue"
import Roletable from "./Roletable.vue"
import Menutable from "./Menutable.vue"
import Hospitaltable from "./Hospitaltable.vue"

defineOptions({
  name: 'componentstable',
})
//父组件传过来，标题name，表格name，搜索name
const props = defineProps<{
  tabletitle: { name: string; istable: string; search: string }
}>()
const tabletitle = ref(props.tabletitle)
//搜索框
const input = ref('')
//声明组件唯一key，触发从新渲染
const keycomponent = ref(1)
//子组件name
const componentname = ref(props.tabletitle.istable)
//动态子组件
const tabs = {
  organizationtable,
  Userlisttable,
  Roletable,
  Menutable,
  Hospitaltable
}
//声明弹出层状态
const someData = ref({
  state:'',//增删改
  data: {}, //弹出层表单
})
//子传父
const emit = defineEmits(['gettableValue'])
//新增
const add = (state: string) => {
  getinit(state)
}
//双击行或者多选
const handleSelecValue = (data: any) => { 
  if(data.name == "click"){
    getinit('upd',data.val)
  }else{
    console.log(data,"多选选中");
  }
}
//增删改方法
const getinit = (state: string,data?: string) => {
  someData.value = {
    state: state,
    data: data || {}
  }
  //传递状态，回显内容
  emit('gettableValue', true, someData.value)
  keycomponent.value += 1
}
</script>

<template>
  <div class="table_heade">
    <div class="header">{{ tabletitle.name }}</div>
    <div class="operate">
      <div class="li">
        <span>{{ tabletitle.search }}</span>
        <el-input
          v-model="input"
          style="width: 200px"
          placeholder="输入科室名称"
        />
        <el-button type="primary" plain>检索</el-button>
      </div>
      <div class="li">
        <el-button type="primary" plain @click="add('add')">新增</el-button>
        <el-button type="danger" plain>删除</el-button>
      </div>
    </div>
    <!-- 动态子组件 -->
    <component :key="keycomponent" :is="tabs![componentname as keyof typeof tabs]" @handleSelec="handleSelecValue"></component>
    
  </div>
</template>

<style lang="scss" scoped>
@mixin flex {
  display: flex;
  align-items: center;
}
.table_heade {
  background-color: #fff;
  .header {
    font-size: 18px;
    font-weight: 600;
    border-left: 5px solid #466cfa;
    padding-left: 15px;
  }
  .operate {
    margin: 5px 0;
    @include flex;
    justify-content: space-between;
    span {
      min-width: 80px;
    }
    .li {
      .el-button {
        margin-left: 10px;
      }
      @include flex;
    }
  }
  :deep(.is-leaf){
    border-color: #ccc;
    background-color: rgb(204, 218, 248) !important;
  }
}
</style>
